package com.flower.service.impl;

import com.flower.entity.Admin;
import com.flower.mapper.AdminMapper;
import com.flower.service.AdminService;
import java.util.List;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.io.Serializable;
import java.util.Arrays;
/**
 * AdminServiceImpl 服务接口实现类
 * @date 2019-03-03 10:28:33
 * @version 1.0
 */

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
	@Autowired
	private AdminMapper adminMapper;

	/** 添加方法 */
	public void save(Admin admin){
		try {
			adminMapper.insertSelective(admin);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 修改方法 */
	public void update(Admin admin){
		try {
			adminMapper.updateByPrimaryKeySelective(admin);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 根据主键id删除 */
	public void delete(Serializable id){
		try {
			adminMapper.deleteByPrimaryKey(id);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 批量删除 */
	public void deleteAll(Serializable[] ids){
		try {
			// 创建示范对象
			Example example = new Example(Admin.class);
			// 创建条件对象
			Example.Criteria criteria = example.createCriteria();
			// 创建In条件
			criteria.andIn("id", Arrays.asList(ids));
			// 根据示范对象删除
			adminMapper.deleteByExample(example);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 根据主键id查询 */
	public Admin findOne(Serializable id){
		try {
			return adminMapper.selectByPrimaryKey(id);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 查询全部 */
	public List<Admin> findAll(){
		try {
			return adminMapper.selectAll();
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 多条件分页查询 */
	public List<Admin> findByPage(Admin admin, int page, int rows){
		try {
			PageInfo<Admin> pageInfo = PageHelper.startPage(page, rows)
				.doSelectPageInfo(new ISelect() {
					@Override
					public void doSelect() {
						adminMapper.selectAll();
					}
				});
			return pageInfo.getList();
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/**  根据用户名查询信息*/
	@Override
	public Admin findByName(String username) {
		try {
			Admin admin=new Admin();
			admin.setName(username);
			 return adminMapper.selectOne(admin);
		}catch (Exception e){
			throw new RuntimeException("根据用户名查询失败");
		}
	}

	@Override
	public void updateByUsername(String username, String password) {
		try{
			adminMapper.updateByUsername(username,password);
		}catch(Exception e){
			throw new RuntimeException("跟新密码失败");
		}
	}
}